<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <p>1.通过下标修改数组一整项</p>
    <p v-for="(item, index) in arr1" :key="index">{{item}}</p>
    <button @click="changeArr1">修改数组内容</button>
    <hr>
    <p v-for="(item, index) in arr2" :key="index+10">{{item.name}}--{{item.num}}</p>
    <button @click="changeArr2">修改数组内容</button>
    <p>2.通过长度直接修改数组</p>
    <p v-for="(item, index) in arr1" :key="index+20">{{item}}</p>
    <button @click="changeArr3">修改数组长度</button>
  </div>
  <script src="../vue.js"></script>
  <script>
    let vm = new Vue({
      el: '#app',
      data: {
        arr1: [1,2,3,4],
        arr2: [
          {name: 'iphone 13', num: 12},
          {name: 'mi 11', num: 120},
          {name: 'p40 pro', num: 80}
        ]
      },
      methods: {
        changeArr1() {
          // this.arr1[3] = 6; 错误的方式
          this.arr1.splice(3, 1, '6'); // 正确的方式1
          console.log(this.arr1);
        },
        changeArr2() {
          // this.arr2[0] = {name: 'Reno Ace', num: 657}; 错误的方式
          // this.$set(this.arr2, 0, {name: 'Reno Ace', num: 657}); // 正确的方式2
          Vue.set(this.arr2, 0, {name: 'Reno Ace', num: 657}); // 正确的方式2.5
          console.log(this.arr2);
        },
        changeArr3() {
          // this.arr1.length = 1; 错误的方法
          this.arr1.splice(1); // 正确的方式
          console.log(this.arr1);
        }
      },
    })
  </script>
</body>
</html>